package com.servlet;

import com.domian.Station;
import com.domian.Tag;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class Getpoint {
Map<String,List<Tag>>tagMap=new HashMap<>();
List<Station>stationList=new ArrayList<>();
    List<Mypoint>points=new ArrayList<>();
    List<Mypoint>pointsjiaozhun=new ArrayList<>();

    public List<Mypoint> getPointsjiaozhun() {
        return pointsjiaozhun;
    }

    public void setPointsjiaozhun(List<Mypoint> pointsjiaozhun) {
        this.pointsjiaozhun = pointsjiaozhun;
    }

    int Tag;
    public Map<String, List<com.domian.Tag>> getTagMap() {
        return tagMap;
    }

    public void setTagMap(Map<String, List<com.domian.Tag>> tagMap) {
        this.tagMap = tagMap;
    }

    public List<Station> getStationList() {
        return stationList;
    }

    public void setStationList(List<Station> stationList) {
        this.stationList = stationList;
    }

    public List<Mypoint> getPoints() {
        return points;
    }

    public void setPoints(List<Mypoint> points) {
        this.points = points;
    }

    public int getTag() {
        return Tag;
    }

    public void setTag(int tag) {
        Tag = tag;
    }
    public synchronized void getvalue(String str,String ip){
        String header;
        int sid;
        int tid;
        int data0;
        int data1;
        int data2;
        int data3;
        if (str.length()>=63) {
            header=(str.substring(0, 2));
            data0=Integer.parseInt(str.substring(6, 14),16);
            data1=Integer.parseInt(str.substring(15, 23),16);
            data2=Integer.parseInt(str.substring(24, 32),16);
            data3=Integer.parseInt(str.substring(33, 41),16);
            tid= Integer.parseInt((str.substring(60, 61)));
            sid= Integer.parseInt((str.substring(62, 63)));
            if (header.equals("ma"))
            {
                Station station=new Station();
                station.setS0(new Mypoint(0,0));
                station.setS1(new Mypoint(data1,0));
                double X=
                        (double) (data1*data1+data2*data2-data3*data3)/(2*data1);
                station.setS2(new Mypoint(X,
                        Math.pow(data2*data2-(X*X),0.5)));
                stationList.add(station);
                while (stationList.size()>10) {
                    stationList.remove(0);
                }
            }
            if (header.equals("mc"))
            {

                int length=stationList.size();
                Station achor=new Station();
                for (int i=0;i<length;i++)
                    achor.addstation(stationList.get(i));
                achor.setS0(new Mypoint(achor.getS0(),length));
                achor.setS1(new Mypoint(achor.getS1(),length));
                achor.setS2(new Mypoint(achor.getS2(),length));
                double Y;
                double X=
                        (achor.getS1().x*achor.getS1().x+data0*data0-data1*data1)/(2*achor.getS1().x);
                double Y1=Math.pow(data0*data0-X*X,
                        0.5);
                double Y2=-Math.pow(data0*data0-X*X,
                        0.5);
                if (Math.abs((achor.getS2().x-X)*(achor.getS2().x-X)
                        +(achor.getS2().y-Y1)*(achor.getS2().y-Y1)-data2*data2)<=
                        Math.abs((achor.getS2().x-X)*(achor.getS2().x-X)
                                +(achor.getS2().y-Y2)*(achor.getS2().y-Y2)-data2*data2))
                     Y=Y1;
                else Y=Y2;
                points.add(new Mypoint(X,Y));
                X=0;
                Y=0;
                if (points.size()<20) {
                    for (int i = 0; i < points.size(); i++) {
                        X += points.get(i).x;
                        Y += points.get(i).y;
                    }
                    X=X/points.size();
                    Y=Y/points.size();
                }
                else {
                    for (int i = points.size()-20; i < points.size(); i++) {
                        X += points.get(i).x;
                        Y += points.get(i).y;
                    }
                    X=X/20;
                    Y=Y/20;
                }
                pointsjiaozhun.add(new Mypoint(tid,X,Y));
                System.out.println("["+X+","+Y+"],");
            }
        }
    }
}